package com.example.searchcenter.center;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.searchcenter.R;
import com.example.searchcenter.helper.JSONParser;
import com.example.searchcenter.helper.TabsPagerAdapter;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;


/***
 * 
 * @author NguyenTrung
 * function: View a detail of places. incluces: "Vị trí", "Chi tiết", "Hình ảnh", "Hoạt động"
 */
@SuppressLint("NewApi")
public class DetailsCenter extends FragmentActivity implements
		ActionBar.TabListener {

	TextView Name;
	TextView center_id;
	TextView des;
	// variable static for fragments
	public static String id_center;
	public static String name_center;
	public static String thumbnaiImage;
	public static String no;
	public static String ward;
	public static String street;
	public static String district;
	public static String city;
	public static String description;
	public static String create_date;
	public static String phone;
	public static String presenter;
	public static String website;
	public static String email;
	public static String type;
	public static String place_lat;
	public static String place_long;
	
	
	// JSON parser class
	JSONParser jsonParser = new JSONParser();


	// single product url
	private static final String url_details = "http://tinhnguyen.edu.vn/apis/getPlaceDetails";

	// JSON Node names
	static final String TAG_SUCCESS = "success";
	static final String TAG_CENTERS = "center";
	static final String TAG_ID_CENTER = "cid";
	static final String TAG_NAME = "name";
	static final String TAG_THUMBNAIL = "thumbnailImageUrl";
	static final String TAG_NO = "no";
	static final String TAG_STREET = "street";
	static final String TAG_WARD = "ward";
	static final String TAG_DISTRICT = "district";
	static final String TAG_CITY = "city";
	static final String TAG_CREATEDATE = "createDate";
	static final String TAG_DESCRIPTION = "description";
	static final String TAG_PRESENTER = "presenter";
	static final String TAG_PHONE = "phone";
	static final String TAG_WEBSITE = "website";
	static final String TAG_EMAIL = "email";
	static final String TAG_TYPE = "type";
	static final String TAG_LAT = "lat";
	static final String TAG_LONG = "long";

	private ViewPager viewPager;
	private TabsPagerAdapter mAdapter;
	private ActionBar actionBar;
	// Tab titles
	private String[] tabs = { "Vị trí", "Chi tiết", "Hình ảnh", "Hoạt động" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_center);
		// check version sdk
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		
		// getting center id from intent
		Intent i = getIntent();
		id_center = i.getStringExtra(TAG_ID_CENTER);
		
		// getting center from JSON
		int success;

		try {
			// Building Parameters
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("id", id_center));
	
			// getting product details by making HTTP request
			// Note that product details url will use GET request
			JSONObject json = jsonParser.makeHttpRequest(
					url_details, "GET", params);

			// check your log for json response
			Log.d("Single Center Details", json.toString());

			// json success tag
			success = json.getInt(TAG_SUCCESS);
			
			if (success == 1) {
				// successfully received product details
				JSONArray productObj = json
						.getJSONArray(TAG_CENTERS); // JSON Array

				// get first product object from JSON Array
				JSONObject center = productObj.getJSONObject(0);
				name_center = center.getString(TAG_NAME);
				description = center.getString(TAG_DESCRIPTION);
				create_date = center.getString(TAG_CREATEDATE);
				thumbnaiImage = center.getString(TAG_THUMBNAIL);
				no = center.getString(TAG_NO);
				street = center.getString(TAG_STREET);
				ward = center.getString(TAG_WARD);
				district = center.getString(TAG_DISTRICT);
				city = center.getString(TAG_CITY);
				website = center.getString(TAG_WEBSITE);
				presenter = center.getString(TAG_PRESENTER);
				phone = center.getString(TAG_PHONE);
				email = center.getString(TAG_EMAIL);
				type = center.getString(TAG_TYPE);
				place_lat = center.getString(TAG_LAT);
				place_long = center.getString(TAG_LONG); 
				
				Log.w("lat", place_lat);
				Log.w("image", thumbnaiImage);
				//Toast.makeText(getApplicationContext(), place_lat, Toast.LENGTH_LONG).show();
			} else {
				// center with cid not found
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		///////////////
		// Initilization
		viewPager = (ViewPager) findViewById(R.id.pager);
		actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
		viewPager.setAdapter(mAdapter);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
		// Adding Tabs
		for (String tab_name : tabs) {
			actionBar.addTab(actionBar.newTab().setText(tab_name)
					.setTabListener(this));
		}
		
		/**
		 * on swiping the viewpager make respective tab selected
		 * */
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// on changing the page
				// make respected tab selected
				actionBar.setSelectedNavigationItem(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		
		
	}
	
	

	@Override
	public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction arg1) {
		// on tab selected
		// show respected fragment view
		viewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.detail_center_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}
	/**
	 * On select action bar icon 
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		// take appropriate action for each item click
		switch (item.getItemId()){
		case R.id.action_share_place :
			Intent share =  new Intent(android.content.Intent.ACTION_SEND);
			share.setType("text/plain");
			share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
			// add data to intent
			share.putExtra(Intent.EXTRA_SUBJECT, "Share to Facebook");
			share.putExtra(Intent.EXTRA_TEXT, "http://tinhnguyen.edu.vn/places/detail/"+id_center);
			startActivity(Intent.createChooser(share, name_center));
			return true;
		case R.id.action_input_direction :
			Intent input = new Intent(DetailsCenter.this, MapsDirection.class);
			input.putExtra(TAG_LAT, place_lat);
			input.putExtra(TAG_LONG, place_long);
			startActivity(input);
			return true;
		default: return super.onOptionsItemSelected(item);
		}
	}
}